
#import "Bottle_Game.h"
#import "GameConfig.h"
#import "AppController.h"

static int gCircle = 360;

typedef enum
{
	EBottle_Layer_Shadow = 100,
	EBottle_Layer_Count
}EBottle_Layer;

@implementation Bottle_Game

+ (id)scene
{
	CCScene     *scene = [CCScene node];
	Bottle_Game *layer = [Bottle_Game node];
	[scene addChild:layer];
	return scene;
}

-(void)Bottle_Game_Result:(id)sender
{
	[self unscheduleUpdate];
	
	[SimpleAudioEngine sharedEngine].effectsVolume = 1.0f;
	
	if(delegate)
	{
		[delegate Bottle_GameSelect:EBottle_Game_Result];
	}
}

-(void)Bottle_Game_Return:(id)sender
{
	[[SimpleAudioEngine sharedEngine] stopEffect:m_nTurningSoundID];
	
	[SimpleAudioEngine sharedEngine].effectsVolume = 1.0f;
	
	[self unscheduleUpdate];
	
	if(delegate)
	{
		[delegate Bottle_GameSelect:EBottle_Game_Return];
	}
}

-(void)Bottle_Game_Des:(id)sender
{
	[SimpleAudioEngine sharedEngine].effectsVolume = 1.0f;
	
	if(delegate)
	{
		[delegate Bottle_GameSelect:EBottle_Game_Des];
	}
}

-(id)init:(id<Bottle_GameDelegate>) _delegate
{
	if((self = [super init])) 
	{
		delegate = _delegate;
		
		AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
		
		CGSize size = [[CCDirector sharedDirector]winSize];
		
		//LOGO
		CCSprite * spLogo=[CCSprite spriteWithFile:@"logo2.png"];
		spLogo.position = ccp(SCREEN_WIDTH/2,460);
		[self addChild:spLogo];
		
		//tip
		spTip=[CCSprite spriteWithFile:@"bottle_game_tip.png"];
		spTip.position = ccp(SCREEN_WIDTH/2,410);
		[self addChild:spTip];
		
		//8人
		numLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d 人",app.nBottle_PlayerNum]
												  fontName:@"Arial" fontSize:24];
		[numLabel setPosition: ccp(SCREEN_WIDTH/2,410)];
		[numLabel setVisible:NO];
		[self addChild: numLabel];
		
		
		//酒瓶背景
		bottomNumBg=[CCSprite spriteWithFile: [NSString stringWithFormat:@"bottle_bg_%d.png",app.nBottle_PlayerNum]];
		bottomNumBg.position = ccp(SCREEN_WIDTH/2,SCREEN_HEIGHT/2);
		[self addChild:bottomNumBg];
		
		bottomNumBg_dark=[CCSprite spriteWithFile: [NSString stringWithFormat:@"bottle_bg_%d_dark.png",app.nBottle_PlayerNum]];
		bottomNumBg_dark.position = ccp(SCREEN_WIDTH/2,SCREEN_HEIGHT/2);
		[self addChild:bottomNumBg_dark];
		[bottomNumBg_dark setVisible:NO];
		
		
		//底部背景
		CCSprite *bottomBg=[CCSprite spriteWithFile:@"bottom_bg.png"];
		bottomBg.position = ccp(SCREEN_WIDTH/2,BOTTOM_HEIGHT);
		[self addChild:bottomBg];
		
		//返回
		CCMenuItemImage *vm = [CCMenuItemImage itemFromNormalImage:@"back_icon.png" selectedImage:nil target:self selector:@selector(Bottle_Game_Return:)];	
		CCMenu *vmm = [CCMenu menuWithItems: vm, nil];
		vmm.position = ccp(BOTTOM_X, BOTTOM_HEIGHT);
		[self addChild:vmm z:2];
		
		//help
		CCMenuItemImage *help = [CCMenuItemImage itemFromNormalImage:@"help_icon.png" selectedImage:nil target:self selector:@selector(Bottle_Game_Des:)];	
		CCMenu *helpMenu = [CCMenu menuWithItems: help, nil];
		helpMenu.position = ccp(SCREEN_WIDTH - BOTTOM_X , BOTTOM_HEIGHT);
		[self addChild:helpMenu z:2];
		
		
		bottleTip1 = [CCSprite spriteWithFile:@"bottle_tip1.png"];
		bottleTip1.position = ccp(size.width/2 - 50,size.height/2);
		[self addChild:bottleTip1];
		
		bottleTip2 = [CCSprite spriteWithFile:@"bottle_tip2.png"];
		bottleTip2.position = ccp(size.width/2 + 50,size.height/2);
		[self addChild:bottleTip2];
		
		//特效
		bottleEffect = [CCSprite spriteWithFile:@"bottle_turn_effect.png"];
		bottleEffect.position = ccp(size.width/2,size.height/2);
		[bottleEffect setVisible:NO];
		[self addChild:bottleEffect];
		
		
		roundDisk = [CCSprite spriteWithFile:@"bottle.png"];
		roundDisk.position = ccp(size.width/2,size.height/2);
		
		[self addChild:roundDisk z:10];
		diskRotation = 0;
		self.isTouchEnabled = YES;
		
		//b_IsTurning  = NO;
		
		[self scheduleUpdate];
		
		//弹出惩罚
		punishbg=[CCSprite spriteWithFile:@"按钮背景.png"];
		punishbg.position = ccp(size.width/2,24);
		[self addChild:punishbg z:1];
		
		[punishbg setVisible:NO];
		
		CCMenuItem *Game1 = [CCMenuItemImage itemFromNormalImage:@"接受惩罚.png"
												   selectedImage:@"接受惩罚_down.png" 
														  target:self 
														selector:@selector(Bottle_Game_Result:)];
		
		//CCMenuItem *Game1 = [CCMenuItemFont itemFromString:@"弹出惩罚" target:self selector:@selector(Bottle_Game_Result:)];
		
		m_pResult = [CCMenu menuWithItems:Game1, nil];
		
		m_pResult.position = ccp(size.width/2,BOTTOM_HEIGHT);
		
		[m_pResult setVisible:NO];
		
		[self addChild:m_pResult z:1];
		
		
		m_nState = EBottle_State_Init;
		
		m_nEffectOpacity = 255.0f;
		
		m_nTurningSoundValume = 1.0f;
	}
	return self;
}

- (void)dealloc
{
	[super dealloc];
}

- (NSInteger)getPunishNum:(int)pAngle
{
	NSInteger pIndex = 0;
	
	AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
	
	//int perCircle = gCircle / app.nBottle_PlayerNum;
	
	switch (app.nBottle_PlayerNum)
	{
		case 2:
		{
			if(0 <= pAngle && pAngle < 180)
			{
				pIndex = 2;
			}
			else 
			{
				pIndex = 1;
			}

		}
			break;
		case 3:
		{
			if(0 <= pAngle && pAngle < 120)
			{
				pIndex = 2;
			}
			else if(120 <= pAngle && pAngle < 240)
			{
				pIndex = 3;
			}
			else 
			{
				pIndex = 1;
			}
		}
			break;
		case 4:
		{
			if(45 <= pAngle && pAngle < 135)
			{
				pIndex = 2;
			}
			else if(135 <= pAngle && pAngle < 225)
			{
				pIndex = 3;
			}
			else if(225 <= pAngle && pAngle < 315)
			{
				pIndex = 4;
			}
			else 
			{
				pIndex = 1;
			}
		}
			break;
		case 5:
		{
			if(0 <= pAngle && pAngle < 72)
			{
				pIndex = 2;
			}
			else if(72 <= pAngle && pAngle < 144)
			{
				pIndex = 3;
			}
			else if(144 <= pAngle && pAngle < 216)
			{
				pIndex = 4;
			}
			else if(216 <= pAngle && pAngle < 288)
			{
				pIndex = 5;
			}
			else 
			{
				pIndex = 1;
			}
		}
			break;
		case 6:
		{
			if(0 <= pAngle && pAngle < 60)
			{
				pIndex = 2;
			}
			else if(60 <= pAngle && pAngle < 120)
			{
				pIndex = 3;
			}
			else if(120 <= pAngle && pAngle < 180)
			{
				pIndex = 4;
			}
			else if(180 <= pAngle && pAngle < 240)
			{
				pIndex = 5;
			}
			else if(240 <= pAngle && pAngle < 300)
			{
				pIndex = 6;
			}
			else 
			{
				pIndex = 1;
			}
		}
			break;
		case 7:
		{
			if(0 <= pAngle && pAngle < 51)
			{
				pIndex = 2;
			}
			else if(51 <= pAngle && pAngle < 102)
			{
				pIndex = 3;
			}
			else if(102 <= pAngle && pAngle < 153)
			{
				pIndex = 4;
			}
			else if(153 <= pAngle && pAngle < 204)
			{
				pIndex = 5;
			}
			else if(204 <= pAngle && pAngle < 255)
			{
				pIndex = 6;
			}
			else if(255 <= pAngle && pAngle < 306)
			{
				pIndex = 7;
			}
			else 
			{
				pIndex = 1;
			}
		}
			break;
		case 8:
		{
			if(0 <= pAngle && pAngle < 45)
			{
				pIndex = 1;
			}
			else if(45 <= pAngle && pAngle < 90)
			{
				pIndex = 2;
			}
			else if(90 <= pAngle && pAngle < 135)
			{
				pIndex = 3;
			}
			else if(135 <= pAngle && pAngle < 180)
			{
				pIndex = 4;
			}
			else if(180 <= pAngle && pAngle < 225)
			{
				pIndex = 5;
			}
			else if(225 <= pAngle && pAngle < 270)
			{
				pIndex = 6;
			}
			else if(270 <= pAngle && pAngle < 315)
			{
				pIndex = 7;
			}
			else 
			{
				pIndex = 8;
			}
		}
			break;
		default:
			break;
	}
	
	return pIndex;
}

- (void)update:(ccTime)delta
{
	roundDisk.rotation = diskRotation;//调整roundDisk偏转角度
	
	//CCLOG(@"update[%f]",angleBeforeTouchesEnd);
	
	if (fabs(angleBeforeTouchesEnd)>0.02)
	{
		angleBeforeTouchesEnd = angleBeforeTouchesEnd * 0.99;
		diskRotation += angleBeforeTouchesEnd;
		
		//特效
		
		m_nEffectOpacity = m_nEffectOpacity * 0.99;
		
		m_nTurningSoundValume = m_nTurningSoundValume * 0.99;
		
		//CCLOG(@"[%f]",m_nEffectOpacity);
		
		bottleEffect.opacity = m_nEffectOpacity;
		
		[SimpleAudioEngine sharedEngine].effectsVolume = m_nTurningSoundValume;
		
	}
	else
	{
		angleBeforeTouchesEnd = 0;
		//if (b_IsTurning)
		if(m_nState == EBottle_State_Turning)
		{
			CGSize size = [[CCDirector sharedDirector]winSize];
			AppController * app = (AppController*)[[UIApplication sharedApplication] delegate];
			
			int ground  = roundDisk.rotation;
			int pResult = ground % gCircle;
			
			if(pResult < 0)
			{
				pResult = pResult + 360;
			}
			
			CCLOG(@"Turn_End[%d]",pResult);
			
			//b_IsTurning = NO;
			
			int pNum = [self getPunishNum:pResult];
			
			[numLabel setString:[NSString stringWithFormat:@"%d 号",pNum]];
			
			m_nState = EBottle_State_Done;
			
			[m_pResult    setVisible:YES];
			//[punishbg     setVisible:YES];
			
			[bottleEffect setVisible:NO];
			
			[[SimpleAudioEngine sharedEngine] stopEffect:m_nTurningSoundID];
			
			
			CCSprite * pShadow=[CCSprite spriteWithFile:[NSString stringWithFormat:@"shadow_%d_%d.png",app.nBottle_PlayerNum,pNum]];
			pShadow.position = ccp(size.width/2,size.height/2);
			[self addChild:pShadow z:8 tag:EBottle_Layer_Shadow];
			
		}
	}
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	angleBeforeTouchesEnd = 0;
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	
	CGPoint firstLocation = [touch previousLocationInView:[touch view]];
	CGPoint location = [touch locationInView:[touch view]];
	

	CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
	CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
	
	CGPoint firstVector = ccpSub(firstTouchingPoint, roundDisk.position);//计算出两点之间的向量
	CGFloat firstRotateAngle = -ccpToAngle(firstVector);//计算出弧度，结果等于 x＊M_PI
	CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);//转换成度
	
	CGPoint vector = ccpSub(touchingPoint, roundDisk.position);//
	CGFloat rotateAngle = -ccpToAngle(vector);//
	CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);//
	angleBeforeTouchesEnd = currentTouch - previousTouch;

	diskRotation += currentTouch - previousTouch;//
	
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	//CCLOG(@"angleBeforeTouchesEnd[%f]",angleBeforeTouchesEnd);
	if (fabs(angleBeforeTouchesEnd) > 3.0f)
	{
		//b_IsTurning = YES;
		m_nState = EBottle_State_Turning;
		
		[m_pResult setVisible:NO];
		
		[self removeChild:spTip cleanup:YES];
		[numLabel setVisible:YES];
		
		[bottleTip1 setVisible:NO];
		[bottleTip2 setVisible:NO];
		
		m_nEffectOpacity = 255.0f;
		
		bottleEffect.opacity = m_nEffectOpacity;
		
		[bottleEffect setVisible:YES];
		
		
		m_nTurningSoundValume = 1.0f;
		
		[SimpleAudioEngine sharedEngine].effectsVolume = m_nTurningSoundValume;
		
		m_nTurningSoundID = [[SimpleAudioEngine sharedEngine] playEffect:@"turning.caf"];
		
		CCNode * pShadow = [self getChildByTag:EBottle_Layer_Shadow];
		if(pShadow)
		{
			[self removeChild:pShadow cleanup:YES];
		}
		
		//[bottomNumBg	  setVisible:NO];
		[bottomNumBg_dark setVisible:YES];
	}
}

- (void)layerReplaced
{
	
}

@end
